home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 282_01 / hod.c < prev    next >
Text File  |  1989-01-11  |  23KB  |  868 lines

  1. /*
  2. TITLE:        HODGEPODGE;
  3. DATE:        8/30/88;
  4. DESCRIPTION:    "Makes waves.";
  5. VERSION:    1.01;
  6. KEYWORDS:    HODGEPODGE, Waves;
  7. FILENAME:    HOD.C;
  8. WARNINGS:    "Requires Hercules monochrome graphics. Hard disk recommended.";
  9. SEE-ALSO:    HOD.DOC;
  10. SYSTEM:        MS-DOS;
  11. COMPILERS:    Aztec;
  12. AUTHORS:    Dan Schechter;
  13. REFERENCES:
  14.     AUTHORS:    A.K. Dewdney;
  15.     TITLE:        "Computer Recreations";
  16.     CITATION:    "Scientific American, 259, pp 104-107 (August 1988)."
  17. ENDREF;
  18.  */
  19.  
  20. #define VERSION "Version 1.01 8/30/88"
  21.  
  22. /* Notes to programmers:
  23.  
  24.     This program compiles under Aztec C in large-code/large-data
  25.     model. Some functions are used that may not exist or may have 
  26.     other names in the libraries of other compilers.
  27.     
  28.     scr_getc() causes the program to halt and wait for a key to be
  29.     pressed. It then returns that key. Functions keys and arrow keys
  30.     which DOS returns as a null followed by a character are returned
  31.     as a single character with the high bit set. But that feature
  32.     is not used in this program. It differs from getchar() in that
  33.     it does not wait for the ENTER key to be pressed. Some compilers
  34.     call this function getch().
  35.     
  36.     abstoptr() takes an absolute address in the form of a long
  37.     and returns a long pointer in segment:offset form, which can
  38.     then be used to directly access memory.
  39.     
  40.     time() returns the DOS time and date packed into a long. In
  41.     this program it is used only to seed the random number generator
  42.     and to provide a little extra mixing "exercise" for it. 
  43.     
  44.     clock() returns the number of hundredths of a second since the
  45.     beginning of the present day. In this program it is used only
  46.     to provide setmode() with a one-second pause, needed when
  47.     re-programming the HGA. The return type is a long.
  48.     
  49.     If the argument to either time() or clock() is non-zero, the return
  50.     value will also be placed in the long pointed at by the argument.
  51.     
  52.     bdos() performs INT 21H. AH is set to its first parameter, DX is
  53.     set to its second parameter, and CX is set to its third parameter.
  54.     It returns the value in AL. In this program it is used as an
  55.     inkey() function. inkey() polls the keyboard and returns 0 if
  56.     no key has been pressed, otherwise it returns the value of the
  57.     key that was pressed. It differs from scr_getc() in that inkey()
  58.     does not cause program operation to stop if no key has been pressed.
  59.     
  60.     access() determines the accessibility of a disk file. In this
  61.     program it is only used to check the existance of a file, and as
  62.     called by this program it returns 0 if the named file does not
  63.     exist, and non-zero otherwise.
  64.  
  65.         The first 7 #defines are user-changeable. 
  66.         HEIGHT should not be greater than 80 and WIDTH
  67.         should not be greater than 180, to fit in the
  68.         screen area. Making them smaller will result in
  69.         faster operation. Set MAXFRAME smaller if you have
  70.         limited disk space. If HEIGHT==80 and WIDTH==180
  71.         and MAXFRAME==200 then a full length movie will
  72.         occupy 1440000 bytes of disk space. A good archiving
  73.         program can squeeze that by perhaps half.
  74.         
  75.         K1, K2, and G are the defaults for k1, k2, and g
  76.         respectively. They can be changed at compile time
  77.         or set at run time on the command line. They 
  78.         determine the characteristics of the hodgepodge.
  79.         Likewise for RANDOM, a Kittensoft innovation.
  80.     
  81.     If you compile this program with TABLETEST defined, it will
  82.     display the halftone graphics characters as defined in table.
  83.     It will then wait for you to press any key and then exit.
  84.     Use this feature to view the characters if you want to change
  85.     them.
  86.  
  87.         Exit codes:
  88.             0. Normal termination.
  89.             1. File load failure.
  90.             2. Unrecognized command line option.
  91.             3. Invalid command line parameter value.
  92.  */
  93. #include <string.h>
  94. #include <fcntl.h>
  95.  
  96. #define HEIGHT 80    /* Height of display in 4 X 6 pixel characters. */
  97. #define WIDTH 120    /* Width of display in 4 X 6 pixel characters. */
  98. #define MAXFRAME 200    /* Maximum number of frames in a movie to prevent 
  99.             over-filling your disk. */
  100. #define K1 2        /* Default. May be changed at run time. */
  101. #define K2 3        /* Default. May be changed at run time. */
  102. #define G 25        /* Default. May be changed at run time. */
  103. #define RANDOM 0    /* Default. May be changed at run time. */
  104.  
  105. #define STATENUM 128    /* The number of states. Since the state number
  106.             is divided by 8 to arrive at a display number
  107.             and there are 16 display characters, and since
  108.             disk storage of movies uses one nibble per cell
  109.             STATENUM should not be changed without revising
  110.             the entire display and movie storage schemes. */
  111.  
  112. #define TEXT 0        /* Used for Hercules display. */
  113. #define GRAPHICS 1        /* Used for Hercules display. */
  114. #define INDEXPORT 0x3B4        /* Used for Hercules display. */
  115. #define CONTROLPORT 0x3b8    /* Used for Hercules display. */
  116. #define DATAPORT 0x3b5        /* Used for Hercules display. */
  117. #define CONFIGPORT 0x3bf    /* Used for Hercules display. */
  118. #define G_DELAY 100        /* Used for Hercules display. */
  119. #define FONTADDR 0xffa6e    /* ROM font address. */
  120. #define SCREEN 0xb0000        /* Hercules screen 0 address. */
  121. #define SCREEN1 0xb8000        /* Hercules screen 1 address. */
  122.  
  123. #define HEALTHY 1
  124. #define INFECTED 0
  125. #define ILL -1
  126.  
  127. long time(long *);
  128. void *abstoptr();
  129. int main(int n,char **arg);
  130. void table_init(void);
  131. void screen_put4c(int c1,int c2,int c3, int c4, int v,int h);
  132. int work(unsigned char o[HEIGHT][WIDTH]);
  133. void screenwrite(unsigned char old[HEIGHT][WIDTH],int count);
  134. void sleep(int n);
  135. int rando(void);
  136. int inkey(void);
  137. int infected_neighbors(unsigned char a[HEIGHT][WIDTH],int v,int h);
  138. int ill_neighbors(unsigned char a[HEIGHT][WIDTH],int v,int h);
  139. int n_sum(unsigned char a[HEIGHT][WIDTH],int v,int h);
  140. int health(int a);
  141. void setmode(int q);
  142. int clearscreen(void);
  143. int g_putch(int c,int y);
  144. char *itoa(int n);
  145. int blank(void);
  146. void newname(char *s,int q);
  147. int playback(char *s);
  148. void sw_scr(void);
  149. void g_printf(int v,char *ctrl,...);
  150. int _kdput(char *p,int v);
  151. void erl(void);
  152. int load(unsigned char old[HEIGHT][WIDTH], unsigned char datbuf[HEIGHT*WIDTH], int *count, char *s);
  153. void prompt(void);
  154.  
  155. int gdata[12] = {         /* Constants for programming Hercules card. */
  156.     0x35, 0x2d, 0x2e, 0x7, 0x5b, 
  157.     0x2, 0x57, 0x57, 0x2, 0x3, 0x0, 0x0 
  158. };
  159. int tdata[12] = { 
  160.     0x61, 0x50, 0x52, 0xf, 0x19, 
  161.     0x6, 0x19, 0x19, 0x2, 0xd, 0xb, 0xc 
  162. };
  163. unsigned char table[16][4] = {
  164. /*0*/    { 0,0,0,0 },        /* This table defines the characters        */
  165. /*1*/    { 0,8,0,0 },        /* that will represent the states. Actual   */
  166. /*3*/    { 16,2,8,0 },        /* state numbers are divided by 8,        */
  167. /*4*/    { 8,1,16,2 },        /* requiring 16 display characters.        */
  168. /*6*/    { 40,2,5,16 },        /* Integers in this table must not be        */
  169. /*7*/    { 40,18,5,16 },        /* greater than 63. table_init() will        */
  170. /*9*/    { 36,18,10,37 },    /* shift all these numbers to create tables */
  171. /*10*/    { 42,20,42,17 },    /* 1, 2, 3, and 4 so that                     */
  172. /*12*/    { 42,21,42,21 },    /* characters may be quickly OR'd together  */
  173. /*13*/    { 42,46,42,42 },    /* and written to the display in full       */
  174. /*15*/    { 43,42,43,43 },    /* bytes. You may alter this table        */
  175. /*16*/    { 43,53,45,21 },    /* to change the appearance of the display  */
  176. /*18*/    { 47,53,43,61 },    /* without otherwise affecting program        */
  177. /*20*/    { 47,61,47,61 },    /* operation.                    */
  178. /*22*/    { 63,61,47,63 },
  179. /*24*/    { 63,63,63,63 }
  180. };
  181.  
  182. #define CAT "\n\040\040\040\040\057\134\137\057\134\n\040\040\040\050\376\040\322\040\376\051\n\360\360\360\360\040\304\312\304\040\360\360\360\360\n\040\040\040\040\040\042\042\042"
  183.  
  184. char *HELP = "\
  185. USAGE: HOD [A<k1>] [B<k2>] [G<g>] [M<n>] [I<nn>] [P[<fn>]] [R[<fn>]]\n\
  186. Parameters may be in any order. All are integers.\n\
  187. Defaults: k1=2, k2=3, g=25.\n\n\
  188. <k1> is infected cells divisor.\n\
  189. <k2> is ill cells divisor.\n\
  190. <g> is infection rate.\n\
  191. <nn> is an alternate initializing factor. (1 to number of cells.)\n\
  192. <n> is a randomizing factor. (1 to number of cells.)\n\n\
  193. R tells the program you want to load a file.\n\
  194. P tells the program you want to play back a movie.\n\
  195. <fn> is a file name to load or play back.\n\n\
  196. During program operation:\n\
  197. # (crosshatch) Toggles movie recording.\n\
  198. $ to set